package com.wpq.netty.discard;

import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.util.ReferenceCountUtil;

/**
 *  处理服务端
 */
public class DiscardServerHandler extends ChannelInboundHandlerAdapter {

     @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg){
       //默默的丢弃收到的数据
       //  ((ByteBuf)msg).release();
         // 引用计数对象 处理器的职责是释放所有传递到处理器的引用计数对象
         ByteBuf in =(ByteBuf)msg;
         try {
             while (in.isReadable()){
                 System.out.print((char)in.readByte());
                 System.out.flush();
             }
         }finally {
             ReferenceCountUtil.release(msg);
         }
    }
    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause){

        //当出现异常就关闭连接
        cause.printStackTrace();
        ctx.close();
    }
}
